<?php
namespace App\Logic;


use App\Http\Logic\BaseLogic;
use App\Jobs\NoticeUser;
use App\Models\SwtDheGood;
use App\Models\SwtDheOrderGoods;
use App\Models\SwtGood;
use App\Models\SwtMachineGood;
use App\Models\SwtOrder;
use App\Models\SwtOrderGoods;


class GoodsLogic extends BaseLogic {

    /**
     * @notes 获取订单商品
     * @param $order_id
     * @param $source_type
     * @param $goods
     * @return array
     * @author Evil丿
     * @date 2025/01/10 16:53
     */
    public static function getOrderGoodsList($order_id, $source_type, $goods)
    {
        $goods_list = [];

        $amount = 0;

        switch($source_type) {
            case 1: //大黄鹅
                foreach($goods as $good) {
                    $goods_info = SwtGood::find($good['goods_id'])->toArray();
                    $goods_amount = (int) bcmul($goods_info['sell_price'], $good['goods_num']);
                    $good_data = [
                        'order_id'      => $order_id,
                        'goods_name'    => $goods_info['goods_name'],
                        'goods_num'     => $good['goods_num'],
                        'goods_price'   => $goods_amount
                    ];
                    SwtDheOrderGoods::create($good_data);
                    $goods_list[] = [
                        'barcode'   => $goods_info['bar_code'],
                        'name'      => $goods_info['goods_name'],
                        'amount'    => $goods_amount,
                        'quantity'  => $good['goods_num']
                    ];
                    $amount += $goods_amount;
                }
            break;

            case 2:
                foreach($goods as $good) {
                    $goods_info = SwtMachineGood::with('goods') -> find($good['goods_id'])->toArray();
                    $goods_amount = (int) bcmul($goods_info['sell_price'], $good['goods_num']);
                    $goods_list[] = [
                        'barcode'   => $goods_info['goods']['bar_code'],
                        'name'      => $goods_info['goods']['goods_name'],
                        'amount'    => $goods_amount,
                        'quantity'  => $good['goods_num']
                    ];
                    $amount += $goods_amount;
                }
            break;
        }

        return [
            'goods_list' => $goods_list,
            'amount'     => $amount
        ];
    }

    /**
     * @notes 新增订单商品
     * @param $order_id
     * @param $goods_info
     * @param $source_type
     * @return void
     * @author Evil丿
     * @date 2025/01/10 16:52
     */
    public static function addOrderGoods($order_id, $goods_info, $source_type)
    {
        foreach($goods_info as $v) {
            $data = [
                'order_id'      => $order_id,
                'goods_name'    => $v['name'],
                'goods_num'     => $v['quantity'],
                'goods_price'   => $v['amount']
            ];

            switch($source_type) {
                case 1:
                    $goods = self::getGoodsInfo($v['name']);
                break;
                case 2:
                    $goods = self::getGoodsByQrCode($v['barcode']);
                break;
            }

            if($goods) {
                $data['goods_id'] = $goods['id'];
                $data['goods_integral'] = $goods['integral_num'] ?? 0;
                $data['goods_cost_price'] = $goods['cost_price'];
            }

            SwtOrderGoods::create($data);
        }
    }

    /**
     * @notes 获取商品信息
     * @param $goods_name
     * @return false
     * @author Evil丿
     * @date 2025/01/10 16:45
     */
    public static function getGoodsInfo($goods_name)
    {
        $dhe_goods_info = SwtDheGood::where(function($sql) use ($goods_name) {
            $goods_id = "";
            if(strpos($goods_name,'|') !== false) {
                $arr = explode('|',$goods_name);
                if(count($arr) > 1) {
                    $goods_id = $arr[1];
                }
            }
            if($goods_id !== "") {
                $sql -> where('sku_id', $arr[1]);
            } else {
                $sql -> where('goods_name', $goods_name);
            }

        }) -> first();

        if(!$dhe_goods_info || !$dhe_goods_info['bar_code']) {
            return false;
        }

        $bar_codes = explode(',',$dhe_goods_info['bar_code']);

        $goods_info = SwtGood::whereIn('bar_code', $bar_codes)->first();

        if(!$goods_info) {
            return false;
        }

        return $goods_info -> toArray();
    }

    /**
     * @notes 根据条码获取商品
     * @param $barCode
     * @return false
     * @author Evil丿
     * @date 2025/01/10 16:44
     */
    public static function getGoodsByQrCode($barCode)
    {
        $goods_info = SwtGood::firstWhere('bar_code', $barCode);
        if(!$goods_info) {
            return false;
        }
        return $goods_info -> toArray();
    }

    /**
     * @notes 修改设备商品库存
     * @param $order_id
     * @return bool
     * @author Evil丿
     * @date 2025/01/10 16:52
     */
    public static function updateGoodsWeight($order_id)
    {
        $order_info = SwtOrder::with(['goods','machine'])->firstWhere('id', $order_id) -> toArray();

        if(empty($order_info['goods'])) {
            return false;
        }

        if($order_info['machine']['source_type'] == 2) {
            foreach($order_info['goods'] as $v) {
                $good = SwtMachineGood::with('goods') -> where('machine_id', $order_info['machine_id']) -> where('goods_id', $v['goods_id']) -> first();

                if($good -> weight - $v['goods_num'] <= 0) {
                    NoticeUser::dispatch($order_id, 2, '商品'.$good -> goods -> goods_name.'已售罄') -> onQueue('notice_user');
                }

                $good -> decrement('weight', $v['goods_num']);
            }
        }

        return true;
    }

    /**
     * @notes 获取设备内商品
     * @param $machine_id
     * @return array|false
     * @author Evil丿
     * @date 2025/01/10 16:53
     */
    public static function getDoorGoods($machine_id)
    {
        $list = SwtMachineGood::with('goods')->where('machine_id', $machine_id) -> get();
        if($list) {
            return $list -> toArray();
        }
        return false;
    }
}
